home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / SortDemo / SortItem.java < prev   
Encoding:
Java Source  |  1996-04-29  |  5.9 KB  |  247 lines

  1. /*
  2.  * @(#)SortItem.java    1.17f 95/04/10 James Gosling
  3.             1.18  96/4/24  Jim Hagen : use setBackground
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software
  8.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  9.  * without fee is hereby granted. 
  10.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  11.  * for further important copyright and trademark information and to
  12.  * http://java.sun.com/licensing.html for further important licensing
  13.  * information for the Java (tm) Technology.
  14.  * 
  15.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  16.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  17.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  19.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  20.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  21.  * 
  22.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  23.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  24.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  25.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  26.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  27.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  28.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  29.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  30.  * HIGH RISK ACTIVITIES.
  31.  */
  32.  
  33. import java.awt.*;
  34. import java.io.InputStream;
  35. import java.util.Hashtable;
  36. import java.net.*;
  37.  
  38. /**
  39.  * A simple applet class to demonstrate a sort algorithm.
  40.  * You can specify a sorting algorithm using the "alg"
  41.  * attribyte. When you click on the applet, a thread is
  42.  * forked which animates the sorting algorithm.
  43.  *
  44.  * @author James Gosling
  45.  * @version     1.17f, 10 Apr 1995
  46.  */
  47. public class SortItem extends java.applet.Applet implements Runnable {
  48.     /**
  49.      * The thread that is sorting (or null).
  50.      */
  51.     private Thread kicker;
  52.  
  53.     /**
  54.      * The array that is being sorted.
  55.      */
  56.     int arr[];
  57.  
  58.     /**
  59.      * The high water mark.
  60.      */
  61.     int h1 = -1;
  62.  
  63.     /**
  64.      * The low water mark.
  65.      */
  66.     int h2 = -1;
  67.  
  68.     /**
  69.      * The name of the algorithm.
  70.      */
  71.     String algName;
  72.  
  73.     /**
  74.      * The sorting algorithm (or null).
  75.      */
  76.     SortAlgorithm algorithm;
  77.  
  78.     /**
  79.      * Fill the array with random numbers from 0..n-1.
  80.      */
  81.     void scramble() {
  82.     int a[] = new int[size().height / 2];
  83.     double f = size().width / (double) a.length;
  84.     for (int i = a.length; --i >= 0;) {
  85.         a[i] = (int)(i * f);
  86.     }
  87.     for (int i = a.length; --i >= 0;) {
  88.         int j = (int)(i * Math.random());
  89.         int t = a[i];
  90.         a[i] = a[j];
  91.         a[j] = t;
  92.     }
  93.     arr = a;
  94.     }
  95.  
  96.     /**
  97.      * Pause a while.
  98.      * @see SortAlgorithm
  99.      */
  100.     void pause() {
  101.     pause(-1, -1);
  102.     }
  103.  
  104.     /**
  105.      * Pause a while, and draw the high water mark.
  106.      * @see SortAlgorithm
  107.      */
  108.     void pause(int H1) {
  109.     pause(H1, -1);
  110.     }
  111.  
  112.     /**
  113.      * Pause a while, and draw the low&high water marks.
  114.      * @see SortAlgorithm
  115.      */
  116.     void pause(int H1, int H2) {
  117.     h1 = H1;
  118.     h2 = H2;
  119.     if (kicker != null) {
  120.         repaint();
  121.     }
  122.     try {Thread.sleep(20);} catch (InterruptedException e){}
  123.     }
  124.  
  125.     /**
  126.      * Initialize the applet.
  127.      */
  128.     public void init() {
  129.     String at = getParameter("alg");
  130.     if (at == null) {
  131.         at = "BubbleSort";
  132.     }
  133.  
  134.     algName = at + "Algorithm";
  135.     scramble();
  136.  
  137.     resize(100, 100);
  138.     }
  139.  
  140.     /**
  141.      * Paint the array of numbers as a list
  142.      * of horizontal lines of varying lenghts.
  143.      */
  144.     public void paint(Graphics g) {
  145.     int a[] = arr;
  146.     int y = size().height - 1;
  147.  
  148.     // Erase old lines
  149.     g.setColor(getBackground());
  150.     for (int i = a.length; --i >= 0; y -= 2) {
  151.         g.drawLine(arr[i], y, size().width, y);
  152.     }
  153.  
  154.     // Draw new lines
  155.     g.setColor(Color.black);
  156.     y = size().height - 1;
  157.     for (int i = a.length; --i >= 0; y -= 2) {
  158.         g.drawLine(0, y, arr[i], y);
  159.     }
  160.  
  161.     if (h1 >= 0) {
  162.         g.setColor(Color.red);
  163.         y = h1 * 2 + 1;
  164.         g.drawLine(0, y, size().width, y);
  165.     }
  166.     if (h2 >= 0) {
  167.         g.setColor(Color.blue);
  168.         y = h2 * 2 + 1;
  169.         g.drawLine(0, y, size().width, y);
  170.     }
  171.     }
  172.  
  173.     /**
  174.      * Update without erasing the background.
  175.      */
  176.     public void update(Graphics g) {
  177.     paint(g);
  178.     }
  179.  
  180.     /**
  181.      * Run the sorting algorithm. This method is
  182.      * called by class Thread once the sorting algorithm
  183.      * is started.
  184.      * @see java.lang.Thread#run
  185.      * @see SortItem#mouseUp
  186.      */
  187.     public void run() {
  188.     try {
  189.         if (algorithm == null) {
  190.         algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
  191.         algorithm.setParent(this);
  192.         }
  193.         algorithm.init();
  194.         algorithm.sort(arr);
  195.     } catch(Exception e) {
  196.     }
  197.     }
  198.  
  199.     /**
  200.      * Stop the applet. Kill any sorting algorithm that
  201.      * is still sorting.
  202.      */
  203.     public synchronized void stop() {
  204.     if (kicker != null) {
  205.             try {
  206.         kicker.stop();
  207.             } catch (IllegalThreadStateException e) {
  208.                 // ignore this exception
  209.             }
  210.         kicker = null;
  211.     }
  212.     if (algorithm != null){
  213.             try {
  214.         algorithm.stop();
  215.             } catch (IllegalThreadStateException e) {
  216.                 // ignore this exception
  217.             }
  218.     }
  219.     }
  220.  
  221.  
  222.     /**
  223.      * For a Thread to actually do the sorting. This routine makes
  224.      * sure we do not simultaneously start several sorts if the user
  225.      * repeatedly clicks on the sort item.  It needs to be
  226.      * synchronoized with the stop() method because they both
  227.      * manipulate the common kicker variable.
  228.      */
  229.     private synchronized void startSort() {
  230.     if (kicker == null || !kicker.isAlive()) {
  231.         scramble();
  232.         repaint();
  233.         kicker = new Thread(this);
  234.         kicker.start();
  235.     }
  236.     }
  237.  
  238.  
  239.     /**
  240.      * The user clicked in the applet. Start the clock!
  241.      */
  242.     public boolean mouseUp(java.awt.Event evt, int x, int y) {
  243.     startSort();
  244.     return true;
  245.     }
  246. }
  247.